/**
 * 16进制转rgba进制  用于颜色
 * @param {Number || String}  val 16进制颜色 如 #56d6b7
 */

export const getRgba = (val, opacity = 1) => {
  let six = val.split('#')[1];
  let index = 0;
  let raba = [];
  for (let i = 0; i < 3; i++) {
    raba.push(parseInt(six.slice(index, index + 2), 16));
    index = index + 2;
  }
  return `rgba(${raba.join(',')},${opacity})`;
};
/**
 * 16进制颜色加深 变浅
 * @param {Number || String}  hex 16进制颜色 如 #56d6b7
 * @param {Number}  lum 颜色变化程度 如 0 负数为加深 整数为变浅
 */
export const ColorLuminance = (hex, lum) => {
  hex = String(hex).replace(/[^0-9a-f]/gi, '');
  if (hex.length < 6) {
    hex = hex[0] + hex[0] + hex[1] + hex[1] + hex[2] + hex[2];
  }
  lum = lum || 0;

  let rgb = '#';
  let c;
  let i;
  for (i = 0; i < 3; i++) {
    c = parseInt(hex.substr(i * 2, 2), 16);
    c = Math.round(Math.min(Math.max(0, c + c * lum), 255)).toString(16);
    rgb += ('00' + c).substr(c.length);
  }

  return rgb;
};
